import java.math.*;
import java.net.*;
import java.io.*;

/**
 * Server that just has to run and receive data, sending it to it's
 * given player via a servant thread to avoid not being present when
 * needed
 */
public class PlayerServer extends Thread{

    ServerSocket ss;
    Socket connection;
    Player p;

    public PlayerServer(int port, Player p){
        this.p = p;
        try{
            ss = new ServerSocket(port);
            print("Opened server at: " + ss.getInetAddress().getHostName() + 
                  " at port: " + ss.getLocalPort());
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

    public void run(){
        try{
            if(ss != null){
                connection = ss.accept();                

                PlayerServerServant servant = new PlayerServerServant(connection, p);
                servant.start();

                run();
            }
        }
        catch(SocketException e){
            //do nothing
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

    public void kill(){
        try{
            ss.close();
        }
        catch(Exception e){
            //I don't care..
        }
        finally{
            interrupt();            
        }
    }
    
    public void print(String msg){
        if(Utilities.DEBUG){
            System.out.println(msg);
        }
    }
    
    public void print_less(String msg){
        if(Utilities.DEBUG){
            System.out.print(msg);
        }
    }
    
}